//package com.example.demo.security.impl;
//
//import com.example.demo.domain.Resource;
//import com.example.demo.domain.Role;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.access.ConfigAttribute;
//import org.springframework.security.access.SecurityConfig;
//import org.springframework.security.web.FilterInvocation;
//import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.PostConstruct;
//import java.util.*;
//
///**
// * 最核心的地方，就是提供某个资源对应的权限定义，即getAttributes方法返回的结果。 此类在初始化时，应该取到所有资源及其对应角色的定义。
// */
//@Service
//public class CustomInvocationSecurityMetadataSourceService implements
//        FilterInvocationSecurityMetadataSource {
//
//    @Autowired
//    private RoleDao roleDao;
//
//    private static Map<String, Collection<ConfigAttribute>> resourceMap = new HashMap<>();
//
//    @PostConstruct
//    private void loadResourceDefine() {
//        List<Role> result = roleDao.findByProperty(null);
//        if (null != result && !result.isEmpty()) {
//            for (Role role : result) {
//                Set<Resource> resources = role.getResources();
//                if (null != resources && !resources.isEmpty()) {
//                    for (Resource resource : resources) {
//                        String resourceUrl = resource.getResourceUrl();
//                        ConfigAttribute auth = new SecurityConfig(role.getName());
//                        if (resourceMap.containsKey(resourceUrl)) {
//                            Collection<ConfigAttribute> value = resourceMap.get(resourceUrl);
//                            value.add(auth);
//                            resourceMap.put(resourceUrl, value);
//                        } else {
//                            Collection<ConfigAttribute> configAttributes = new ArrayList<ConfigAttribute>();
//                            configAttributes.add(auth);
//                            resourceMap.put(resourceUrl, configAttributes);
//                        }
//                    }
//                }
//            }
//        }
//
//    }
//
//    @Override
//    public Collection<ConfigAttribute> getAttributes(Object o) throws IllegalArgumentException {
//        FilterInvocation filterInvocation = (FilterInvocation) o;
//        if (resourceMap == null) {
//            loadResourceDefine();
//        }
//        Iterator<String> ite = resourceMap.keySet().iterator();
//        while (ite.hasNext()) {
//            String resURL = ite.next();
//            return resourceMap.get(resURL);
//        }
//        return null;
//
//    }
//
//    @Override
//    public Collection<ConfigAttribute> getAllConfigAttributes() {
//        return new ArrayList<ConfigAttribute>();
//    }
//
//    @Override
//    public boolean supports(Class<?> aClass) {
//        return true;
//    }
//}
